//
//  WBComposeBottomView.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/9.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit

// 按钮枚举
enum HMComposeBottomViewType: Int {
    case picture = 1001
    case mention
    case trend
    case emoticon
    case add
}

class WBComposeBottomView: UIView {
    
    // 01-声明闭包
    var closure:((HMComposeBottomViewType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setuoUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setuoUI(){
        backgroundColor = randomColor()
        let pictureButton = addChildButtom(imgName: "compose_toolbar_picture", type: .picture)
        let mentionButton = addChildButtom(imgName: "compose_mentionbutton_background", type: .mention)
        let trendButton = addChildButtom(imgName: "compose_trendbutton_background", type: .trend)
        let emoticonButton = addChildButtom(imgName: "compose_emoticonbutton_background", type: .emoticon)
        let addButton = addChildButtom(imgName: "compose_add_background", type: .add)
        
        // 添加约束
        pictureButton.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureButton.snp.right)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionButton.snp.right)
            make.width.equalTo(emoticonButton)
        }
        
        emoticonButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendButton.snp.right)
            make.width.equalTo(addButton)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(emoticonButton.snp.right)
        }

    }
}

extension WBComposeBottomView{
    fileprivate func addChildButtom(imgName: String, type: HMComposeBottomViewType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.highlighted)
        button.setImage(UIImage(named:imgName), for: UIControlState.normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        addSubview(button)
        return button
    }
}
extension WBComposeBottomView{
    @objc fileprivate func buttonClick(button: UIButton){
        // 03-执行闭包
        // 通过枚举值得到枚举名HMComposeBottomViewTypeHMComposeBottomViewType(rawValue:Int)
        closure?(HMComposeBottomViewType(rawValue: button.tag)!)
    }
}
